package src.utils.resource;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class FileOperationsUtils {

	public synchronized static void writeAppendStringToFile(String fileName, String Datas) {
		// 不覆盖，续写文件
		File file = new File(fileName);
		try {
			FileUtils.writeStringToFile(file, Datas, "UTF-8", true);
		} catch (IOException | UnsupportedCharsetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void overWriteStringToFile(String fileName, String Datas) {
		// 覆盖文件
		File file = new File(fileName);
		try {
			FileUtils.writeStringToFile(file, Datas, "UTF-8", false);
		} catch (IOException | UnsupportedCharsetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String readFileToString(String fileName) {
		// 读取文件流为字符串
		File file = new File(fileName);
		String content = null;
		try {
			content = FileUtils.readFileToString(file, "UTF-8");
		} catch (IOException | UnsupportedCharsetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return content;
	}

	public static byte[] readFileToByteArray(String fileName) {
		// 读取文件流为字符串
		File file = new File(fileName);
		byte[] content = null;
		try {
			content = FileUtils.readFileToByteArray(file);
		} catch (IOException | UnsupportedCharsetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return content;
	}

//	public static String MD5Encode(String Str) {
//		String hexString = "";
//		try {
//			MessageDigest md5 = MessageDigest.getInstance("MD5");
//			byte[] a = md5.digest(Str.getBytes("utf-8"));
//
//			for (byte b : a) {
//				int temp = b & 255;
//				if (temp < 16 && temp >= 0) {
//					// 手动补上一个“0”
//					hexString = hexString + "0" + Integer.toHexString(temp);
//				} else {
//					hexString = hexString + Integer.toHexString(temp);
//				}
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return hexString;
//	}

	public static String getFormateDate(String Date) {
		String content = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);

			Date date = sdf.parse(Date);
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			content = sdf.format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return content;
	}

	public static String getSimpleFormateDate(String formate) {
		SimpleDateFormat sdf = new SimpleDateFormat(formate);
		String content = sdf.format(new Date());
		return content;
	}

	public static void main(String[] args) {
		String Name = "中文字符123阿斯顿马丁abc";
		FileOperationsUtils.writeAppendStringToFile("./src/main/resources/Data_Files/JsonFiles/jjson.json", Name);
		String str = readFileToString("./src/main/resources/Data_Files/JsonFiles/jjson.json");
		System.out.println(str);

	}

}